{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "1d3b6c3e",
   "metadata": {},
   "source": [
    "## 0. 前置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "b1525d2b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7000bfc6",
   "metadata": {},
   "source": [
    "## 1. str类型转换为数字处理\n",
    "哑变量==独热变量==one-hot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "350115ca",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>华东</th>\n",
       "      <th>华北</th>\n",
       "      <th>华南</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      华东     华北     华南\n",
       "0  False  False   True\n",
       "1  False   True  False\n",
       "2   True  False  False"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.get_dummies(['华南', '华北', '华东'])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8cc2a5ac",
   "metadata": {},
   "source": [
    "## 2. 连续数据离散化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "1213e9f8",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_csv(r\"C:\\Users\\zhengyang\\learning\\Python\\src\\data\\gdp_2018.csv\",index_col=0)\n",
    "sr_area=df['Area']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6954bc6c",
   "metadata": {},
   "source": [
    "### 2.1 等宽法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "b7b70cfa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "No\n",
       "1       6340.50\n",
       "2      16800.00\n",
       "3       1952.84\n",
       "4       7434.40\n",
       "5      82400.00\n",
       "         ...   \n",
       "96     13103.04\n",
       "97      8249.45\n",
       "98     17271.00\n",
       "99     19317.33\n",
       "100     4563.22\n",
       "Name: Area, Length: 100, dtype: float64"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sr_area"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "f839c66e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "No\n",
       "1        (1566.88, 18671.984]\n",
       "2        (1566.88, 18671.984]\n",
       "3        (1566.88, 18671.984]\n",
       "4        (1566.88, 18671.984]\n",
       "5        (69731.996, 86752.0]\n",
       "                ...          \n",
       "96       (1566.88, 18671.984]\n",
       "97       (1566.88, 18671.984]\n",
       "98       (1566.88, 18671.984]\n",
       "99     (18671.984, 35691.988]\n",
       "100      (1566.88, 18671.984]\n",
       "Name: Area, Length: 100, dtype: category\n",
       "Categories (5, interval[float64, right]): [(1566.88, 18671.984] < (18671.984, 35691.988] < (35691.988, 52711.992] < (52711.992, 69731.996] < (69731.996, 86752.0]]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# width_interval是等宽区间\n",
    "width_interval = pd.cut(sr_area,5)\n",
    "width_interval"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "fc014de3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Area\n",
       "(1566.88, 18671.984]      82\n",
       "(18671.984, 35691.988]    13\n",
       "(35691.988, 52711.992]     2\n",
       "(69731.996, 86752.0]       2\n",
       "(52711.992, 69731.996]     1\n",
       "Name: count, dtype: int64"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "width_interval.value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "63fa49b5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>(1566.88, 18671.984]</th>\n",
       "      <th>(18671.984, 35691.988]</th>\n",
       "      <th>(35691.988, 52711.992]</th>\n",
       "      <th>(52711.992, 69731.996]</th>\n",
       "      <th>(69731.996, 86752.0]</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>No</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    (1566.88, 18671.984]  (18671.984, 35691.988]  (35691.988, 52711.992]  \\\n",
       "No                                                                         \n",
       "1                   True                   False                   False   \n",
       "2                   True                   False                   False   \n",
       "3                   True                   False                   False   \n",
       "\n",
       "    (52711.992, 69731.996]  (69731.996, 86752.0]  \n",
       "No                                                \n",
       "1                    False                 False  \n",
       "2                    False                 False  \n",
       "3                    False                 False  "
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 哑变量处理\n",
    "pd.get_dummies(width_interval).head(3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fd7eea2b",
   "metadata": {},
   "source": [
    "### 2.2 等频法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1a931ae1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Area\n",
       "(1651.979, 6626.6]        20\n",
       "(6626.6, 9633.546]        20\n",
       "(9633.546, 12309.462]     20\n",
       "(12309.462, 17454.764]    20\n",
       "(17454.764, 86752.0]      20\n",
       "Name: count, dtype: int64"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "equal_interval = pd.qcut(sr_area,5)\n",
    "equal_interval.value_counts()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "971b3608",
   "metadata": {},
   "source": [
    "### 2.3 土法——等频"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "31729bb2",
   "metadata": {},
   "outputs": [],
   "source": [
    "def SameRateCut(sr_x,k):   # k是离散区间总数。\n",
    "    # percentage是百分比\n",
    "    percentage = np.arange(0,1.001,1.0/k)\n",
    "    # boundaryPoint是边界点\n",
    "    boundary_point=sr_x.quantile(percentage)   \n",
    "    print(boundary_point)\n",
    "    # 下调区间边界起点。此处等号右端只需一个任意正数即可\n",
    "    boundary_point[0]-= 1 \n",
    "    \n",
    "    equal_interval=pd.cut(sr_x, boundary_point)#以特征列sr_x划分等频区间\n",
    "    return equal_interval"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "dcc62f4e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.0     1651.980\n",
      "0.2     6626.600\n",
      "0.4     9633.546\n",
      "0.6    12309.462\n",
      "0.8    17454.764\n",
      "1.0    86752.000\n",
      "Name: Area, dtype: float64\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Area\n",
       "(1650.98, 6626.6]         20\n",
       "(6626.6, 9633.546]        20\n",
       "(9633.546, 12309.462]     20\n",
       "(12309.462, 17454.764]    20\n",
       "(17454.764, 86752.0]      20\n",
       "Name: count, dtype: int64"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "equal_interval = SameRateCut(sr_area,5)\n",
    "equal_interval.value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2f9e2e4c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "mining",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
